/**
 iTextUploader
 (C) 2008 MyselftDev (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: PreferencesData.m 125 2009-05-02 16:10:55Z myselftdev $ */

#import "PreferencesData.h"

#define kPreferenceDataLastFileVersion 3

NSString* encodeURL(CFStringRef string)
{
	CFStringRef result = CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, string, 0, CFSTR(";/?:@&=+$,"), kCFStringEncodingUTF8);
	return [(NSString*) result autorelease];
}

NSInteger checkLock()
{
	NSString *read = [NSString stringWithContentsOfFile:[@LOCK_FILE stringByExpandingTildeInPath] encoding:NSASCIIStringEncoding error:nil];
	
	if(read == nil)
	{
		return 0;
	}
	else
	{
		NSInteger val = [read integerValue];
		if(val < 1)
		{
			return 0;
		}
		else
		{
			return (kill(val, 0)) ? 0 : val;
		}
	}
	
	return 0;
}

@implementation PreferencesData

@synthesize iTextUploaderLiteRunning;

- (id)init
{
	if ((self = [super init]))
	{
		//NSLog(@"Data file: %@", [@DATA_FILE stringByExpandingTildeInPath]);
		
		iTextUploaderLiteRunning = 0;
		
		dataDict = [ [NSMutableDictionary alloc] initWithContentsOfFile:[@DATA_FILE stringByExpandingTildeInPath]];
		if(dataDict == nil)
		{
			[dataDict release];
			
			dataDict = [ [NSMutableDictionary alloc] initWithCapacity:5];
			[dataDict setObject:@"" forKey:@"username"];
			[dataDict setObject:@"" forKey:@"password"];
			[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastupload"];
			[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastid"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"privacymode"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"VIP"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"AutomaticUpload"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"AutomaticUploadNetwork"];
			[dataDict setObject:[NSNumber numberWithUnsignedInt:1] forKey:@"AutomaticUploadInterval"];
			[dataDict setObject:[NSNumber numberWithDouble:0] forKey:@"AutomaticUploadLastExecuted"];
			[dataDict setObject:[NSNumber numberWithUnsignedChar:kPreferenceDataLastFileVersion] forKey:@"FileVersion"];
			
			[self saveData];
		}
		
		if([dataDict objectForKey:@"FileVersion"] == nil)
		{
			// Let's reset the upload counter
			[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastupload"];
			[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastid"];
			
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"VIP"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"AutomaticUpload"];
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"AutomaticUploadNetwork"];
			[dataDict setObject:[NSNumber numberWithUnsignedInt:1] forKey:@"AutomaticUploadInterval"];
			[dataDict setObject:[NSNumber numberWithUnsignedChar:1] forKey:@"FileVersion"];
			
			[self saveData];
		}
		
		if([[dataDict objectForKey:@"FileVersion"] unsignedIntValue] == 1)
		{
			[dataDict setObject:[NSNumber numberWithDouble:0] forKey:@"AutomaticUploadLastExecuted"];
			[dataDict setObject:[NSNumber numberWithUnsignedChar:kPreferenceDataLastFileVersion] forKey:@"FileVersion"];
			
			[self saveData];
		}
	}
	return self;
}

- (id)getData:(NSString*)name
{
	return [dataDict objectForKey:name];
}

- (BOOL)setValue:(id)value forData:(NSString*)name
{
	if([value isKindOfClass:[NSString class]])
	{
		if([name isEqualToString:@"password"] && ![value isEqualToString:[self getData:@"password"]])
		{
			[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"privacymode"];
		}
	}
	
	if([name isEqualToString:@"username"] || [name isEqualToString:@"password"])
	{
		[dataDict setObject:[NSNumber numberWithBool:NO] forKey:@"VIP"];
		[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastupload"];
		[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"lastid"];
	}
	
	if([name isEqualToString:@"AutomaticUploadInterval"])
	{
		[dataDict setObject:[NSNumber numberWithUnsignedInt:0] forKey:@"Counter"];
	}
	
	[dataDict setValue:value forKey:name];
	
	[self saveData];
	
	return YES;
}

- (void)saveData
{
	[dataDict writeToFile:[@DATA_FILE stringByExpandingTildeInPath] atomically:YES];
}

-(void)dealloc
{
	[dataDict release];
	[super dealloc];
}

@end
